﻿using ExcelDataReader;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Luna小店.Utils
{
    public class ExcelUtil
    {
        /// <summary>
        /// 取excel数据
        /// </summary>
        /// <returns></returns>
        public static DataSet getExcelDatas() {
            OpenFileDialog openFile = new OpenFileDialog();
            //openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.Cancel) return null;
            var filePath = openFile.FileName;
            using (var stream = File.Open(openFile.FileName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    // 1. Use the reader methods
                    do
                    {
                        while (reader.Read())
                        {
                            // reader.GetDouble(0);
                        }
                    } while (reader.NextResult());

                    // 2. Use the AsDataSet extension method
                    return reader.AsDataSet();

                }
            }
            return null;
        }
        /// <summary>
        /// 取Excel数据和路径
        /// </summary>
        /// <returns></returns>
        public static ExcelAndPath getExcelDatasAndPath()
        {
            OpenFileDialog openFile = new OpenFileDialog();
            //openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
            openFile.Filter = MIMEConst.EXCEL_MIME;
            openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == DialogResult.Cancel) return null;
            var filePath = openFile.FileName;
            using (var stream = File.Open(openFile.FileName, FileMode.Open, FileAccess.Read))
            {
                using (var reader = ExcelReaderFactory.CreateReader(stream))
                {
                    // 1. Use the reader methods
                    do
                    {
                        while (reader.Read())
                        {
                            // reader.GetDouble(0);
                        }
                    } while (reader.NextResult());

                    // 2. Use the AsDataSet extension method
                    return new ExcelAndPath(reader.AsDataSet(), System.IO.Path.GetDirectoryName(openFile.FileName), System.IO.Path.GetFileName(openFile.FileName));

                }
            }
            return null;
        }
    }
    /// <summary>
    /// 数据和路径
    /// </summary>
    public class ExcelAndPath
    {
        public DataSet DataSet { get; set; }
        public String Path { get; set; }//路径
        public String Name { get; set; }//文件名
        public ExcelAndPath(DataSet dataSet,String Path,String Name)
        {
            this.DataSet = dataSet;
            this.Path = Path;
            this.Name = Name;
        }
    }
}
